<template>
  <div class="serach-result">
    <van-nav-bar :title= "$route.query.keyword + '搜索结果'" left-arrow fixed @click-left="$router.back()"></van-nav-bar>
      <van-list
        class="article-list"
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad">
        <van-cell
          v-for="item in list"
          :key="item.art_id"
          :title="item.title"
          @click="$router.push('/article/' + item.art_id)"/>
      </van-list>
  </div>
</template>

<script>
import { getSearch } from '@/api/search'
export default {
  name: 'searchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      // 1. 调用api, 获取数据
      const result = await getSearch(this.$route.query.keyword, this.page)
      const arr = result.data.data.results
      // console.log(arr);
      // 2. 把数据填充到list
      this.list.push(...arr)
      // 页面自增
      this.page++
      // 3. 修改loading为false
      this.loading = false
      // 4. 判断是否加载完了所有的数据
      if (arr.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
// 样式使得页面出现滚动条
.serach-result {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
</style>
